package com.fxm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fxm.domain.Option;
import com.fxm.domain.Question;
import com.fxm.returns.RespCode;
import com.fxm.returns.RespEntity;
import com.fxm.returns.RespTable;
import com.fxm.service.AnswerService;
import com.fxm.service.OptionService;
import com.fxm.service.QuestionService;
import com.fxm.vo.param.AnswerParam;
import com.fxm.vo.result.AnswerResult;
import com.fxm.vo.result.OptionResult;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 答案管理 服务实现类
 * </p>
 *
 * @author fxm
 * @since 2021-02-22
 */
@Slf4j
@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private OptionService optionService;

    @Override
    public RespTable<List<AnswerResult>> listAnswer(Integer page, Integer limit, String questionTitle) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Question::getDeleted, 0);
        if (StringUtils.isNotBlank(questionTitle)) {
            queryWrapper.lambda().like(Question::getQuestionTitle, questionTitle);
        }
        IPage<Question> iPage = new Page<>(page, limit);
        IPage<Question> pg = questionService.page(iPage, queryWrapper);
        List<Question> questionList = pg.getRecords();
        if (Objects.isNull(questionList) || questionList.size() == 0) {
            return new RespTable(RespCode.OK, null, 0);
        }
        //数据组装
        List<AnswerResult> answerResultList = dataPackage(questionList);
        return new RespTable(RespCode.OK, answerResultList, pg.getTotal());
    }

    /**
     * 数据组装
     * @param questionList
     * @return
     */
    private List<AnswerResult> dataPackage(List<Question> questionList) {
        List<AnswerResult> answerResultList = Lists.newArrayList();
        for (Question question : questionList) {
            AnswerResult answerResult = new AnswerResult();
            answerResult.setId(question.getId());
            answerResult.setQuestionTitle(question.getQuestionTitle());
            answerResult.setQuestionType(question.getQuestionType());
            answerResult.setQuestionAnswer(question.getQuestionAnswer());

            QueryWrapper<Option> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Option::getQuestionId, question.getId())
                                 .eq(Option::getOptionType, question.getQuestionAnswer())
                                 .eq(Option::getDeleted, 0);
            Option option = optionService.getOne(queryWrapper);
            if (Objects.nonNull(option)) {
                answerResult.setOptionContent(option.getOptionContent());
            }

            answerResultList.add(answerResult);
        }
        return answerResultList;
    }

    @Override
    public RespEntity<OptionResult> queryOptionById(Integer id) {
        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
        questionQueryWrapper.lambda().eq(Question::getId, id)
                                     .eq(Question::getDeleted, 0);
        Question question = questionService.getOne(questionQueryWrapper);

        QueryWrapper<Option> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Option::getDeleted, 0)
                             .eq(Option::getQuestionId, id);
        List<Option> optionList = optionService.list(queryWrapper);

        OptionResult optionResult = new OptionResult();
        optionResult.setAnswer(question.getQuestionAnswer());

        for (Option option : optionList) {
            switch (option.getOptionType()) {
                case "A" : optionResult.setOptionA("A：" + option.getOptionContent()); break;
                case "B" : optionResult.setOptionB("B：" + option.getOptionContent()); break;
                case "C" : optionResult.setOptionC("C：" + option.getOptionContent()); break;
                case "D" : optionResult.setOptionD("D：" + option.getOptionContent()); break;
            }
        }

        return new RespEntity(RespCode.SUCCESS, optionResult);
    }

    @Override
    public RespEntity updateAnswer(AnswerParam answerParam) {
        Question question = questionDataMapping(answerParam);
        questionService.updateById(question);
        return new RespEntity(RespCode.SUCCESS);
    }

    /**
     * 题目表 数据映射
     * @param answerParam
     * @return
     */
    private Question questionDataMapping(AnswerParam answerParam) {
        Question question = questionService.getById(answerParam.getId());
        question.setQuestionAnswer(answerParam.getAnswer());
        return question;
    }
}
